package com.ican.dao.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.ican.common.SystemConstants;
import com.ican.common.hibernate.HibernateDao;
import com.ican.common.hibernate.Page;
import com.ican.domain.product.Category;

@Repository("categoryDao")
public class CategoryDao extends HibernateDao<Category, Integer> {

    public List<Category> findCategoryByName(String name){
        return this.find("from Category where name = ?", name);
    }
    public List<Category> findCategoryByCode(String code){
        return this.find("from Category where code = ?", code);
    }
    public List<Category> findCategoryByNameAndCode(String name, String code){
        return this.find("from Category where name = ? and code = ?", new Object[]{name, code});
    }
    public Category findCategoryByNameAndCodeAndLang(String name, String code, Integer langId){
        return this.findUnique("from Category where name = ? and code = ? and langId = ?", new Object[]{name, code, langId});
    }

    public Page<Category> findAllCategory(Page<Category> page, Category category){
        List<String> params = new ArrayList<String>();
        if(category != null){
            if(category.getName() != null && category.getName().length() > 0){
                params.add("name like '%" + category.getName() + "%'");
            }
            if(category.getUrl() != null && category.getUrl().length() > 0){
                params.add("url like '%" + category.getUrl() + "%'");
            }
            if(category.getLangId() != null){
                params.add("langId = " + category.getLangId());
            }
            if(category.getSynStatus() != null && category.getSynStatus() >= 0){
                params.add("synStatus = " + category.getSynStatus());
            }
            if(category.getNcStatus() != null && category.getNcStatus() >= 0){
                params.add("ncStatus = " + category.getNcStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from Category");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.findPage(page, sb.toString());
    }
    @SuppressWarnings("unchecked")
    public List<Integer> findAllCategoryIdsToSynAuto(){
        StringBuffer sb = new StringBuffer("select id from Category where synStatus != ");
        sb.append(SystemConstants.ENTITY_DATE_SYN_STATUS_RUN);
        return this.createQuery(sb.toString()).list();
    }
    
    public void updateCategorySynStatus(Integer cateId, Integer synStatus){
        StringBuffer sb = new StringBuffer("update Category set synTime = :p_syn_time, synStatus = :p_syn_status where id = :p_id");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("p_syn_time", new Date());
        params.put("p_syn_status", synStatus);
        params.put("p_id", cateId);
        this.batchExecute(sb.toString(), params);
    }
    
    public void updateCategorySynCateId(String code, Integer synCategoryId){
        StringBuffer sb = new StringBuffer("update Category set synCategoryId = ");
        sb.append(synCategoryId);
        sb.append(" where code = '");
        sb.append(code);
        sb.append("'");
        this.batchExecute(sb.toString());
    }
    
    public void updateCategoryNcStatus(Integer cateId, Integer ncStatus){
        StringBuffer sb = new StringBuffer("update Category set ncTime = :p_nc_time, ncStatus = :p_nc_status where id = :p_id");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("p_nc_time", new Date());
        params.put("p_nc_status", ncStatus);
        params.put("p_id", cateId);
        this.batchExecute(sb.toString(), params);
    }
    
    public void updateCategoryNcNum(){
        String sql = "{call p_update_product_category_nc_num()}";
        this.createSqlQuery(sql).executeUpdate();
    }
    
    @SuppressWarnings("unchecked")
    public List<Integer> findAllCategoryIdsToNcAuto(){
        String hql = "select id from Category";
        return this.createQuery(hql).list();
    }
}
